/**
 * 发票编辑
 * invoiceModify.js
 * -----------------------------------
 * 18/04/23 Jerry 新增
 */

var
helper = require('../../../utils/helper.js');

Page({

/* ------------------------------
 初始数据
------------------------------ */
data: {
  // invoice 默认留空
  invoice: {},
  types: [
    { val: 'Normal', text: '普通增值税发票' },
    { val: 'special',text: '增值税专用发票'}
  ],
  titleTypes: [
    { val: 'Person', text: '个人' },
    { val: 'Corp', text: '单位' }
  ],
  contentTypes: [
    { val: 'Detail', text: '明细' }
  ]
},
/* ------------------------------
 页面加载
------------------------------ */
onLoad: function(opts){

  // 更新导航
  helper.navTitle('发票信息');

  // 是否来自订单确认
  this.setData({ orderConfirm: 'orderConfirmInvoice' == opts.from });

  // 如果传入了invoiceId，查询详情
  this.queryInvoice(opts.invoiceId);

},
/* ------------------------------
 查询发票详情
------------------------------ */
queryInvoice: function(invoiceId){

  helper.request({
    url: 'wx/invoice/detail',
    data: { invoiceId: invoiceId },
    success: this.bindInvoice
  });

},
/* ------------------------------
 绑定显示发票详情
------------------------------ */
bindInvoice: function(ret){

  // 绑定数据
  helper.setData(this, { invoice: ret }, true);

},
/* ------------------------------
 录入发票类型
------------------------------ */
inputType: function(e){
  this.inputProperty(e, 'type', 'types');
},
/* ------------------------------
 录入抬头类型
------------------------------ */
inputTitleType: function(e){
  this.inputProperty(e, 'titleType', 'titleTypes');
},
/* ------------------------------
 录入抬头名称
------------------------------ */
inputTitleName: function(e){
  this.inputProperty(e, 'titleName', 'titleNames');
},
/* ------------------------------
 录入纳税人识别号
------------------------------ */
inputTaxNumber: function(e){
  this.inputProperty(e, 'taxNumber');
},
/* ------------------------------
 录入发票内容
------------------------------ */
inputContentType: function(e){
  this.inputProperty(e, 'contentType', 'contentTypes');
},
/* ------------------------------
 录入发票属性（私有）
------------------------------ */
inputProperty: function(e, prop, enumName){

  // 更新属性为录入值
  var
  val = e.detail.value,
  invoice = this.data.invoice;

  // 文本框值
  invoice[ prop ] = val;

  // 枚举名称
  if (enumName) {

    // 枚举值
    val = this.data[ enumName ][ val ];
    invoice[ prop ] = val.val;
    invoice[ prop + 'Title' ] = val.text;

  }

  // 绑定数据
  this.setData({ invoice: invoice });

},
/* ------------------------------
 保存发票信息
------------------------------ */
saveInvoice: function(){

  var invoice = this.data.invoice;

  helper.request({
    url: invoice.invoiceId ? 'wx/invoice/modify' : 'wx/invoice/add',
    data: invoice,
    success: this.afterSave
  });

},
/* ------------------------------
 保存发票之后，跳转到指定页面
------------------------------ */
afterSave: function(invoice){

  // 如果来自"订单确认"页，返回 invoiceId 至前一页
  if (this.data.orderConfirm){
    helper.pageArg('orderConfirmInvoice', invoice.invoiceId);
    wx.navigateBack(1);
    return;
  }

  // 默认返回发票列表
  wx.navigateBack();

}

})
